package restaurant.behaviours.kitchen;

import jade.core.behaviours.Behaviour;

import java.util.Hashtable;

import restaurant.agents.KitchenAgent;
import restaurant.core.Request;

public class KitchenCookBehaviour extends Behaviour {
	
	private static final long serialVersionUID = -8920873639361734686L;
	private KitchenAgent agent;
	
	public KitchenCookBehaviour(KitchenAgent agent) {
		this.agent = agent;
	}

	@Override
	public void action() {
		if(this.agent.toCook == null || this.agent.toCook.isEmpty())
			return;

		int table = -1;
		Request toCook = this.agent.toCook.poll();
		long start = System.currentTimeMillis();
		
		/**
		 * Maggiore è l'esperienza è minore il tempo che la kitchen
		 * impiega per fare i pasti.
		 * */
		if (this.agent.getExperience() > 0 && this.agent.getExperience() <= 10)
			agent.bound = 200;
		else if (this.agent.getExperience() > 10 && this.agent.getExperience() <= 40)
			agent.bound = 100;
		else if (this.agent.getExperience() > 40)
			agent.bound = 50;
		for (int i = 0; i < agent.queue.size(); i++) {
			if (agent.queue.get(i).equals("ANTIPASTO")) agent.bound += 1000;
			if (agent.queue.get(i).equals("FIRST")) agent.bound += 2000;
			if (agent.queue.get(i).equals("SECOND")) agent.bound += 1500;
			if (agent.queue.get(i).equals("FRUIT")) agent.bound += 500;
			if (agent.queue.get(i).equals("DESSERT")) agent.bound += 1100;
		}
		for(int i = 0; i < toCook.getMeals().size(); i++) {
			
			while(System.currentTimeMillis() - start < agent.bound);
			this.agent.setExperience(this.agent.getExperience() + 1);
			this.agent.setScore(this.agent.getExperience());

			/**
			 * Una volta che ha assegnato un limite minino al bound deve essere diminuito 
			 * perchè ogni volta che fa un pasto acquisisce esperienza!
			 * */

			Request ready = new Request(toCook.getTable());
			ready.addMeal(toCook.getMeals().get(i));
			table = ready.getTable();

			Hashtable<Request, Boolean> tmp = new Hashtable<Request, Boolean>();
			tmp.put(ready, false);
			this.agent.restaurant.readyMeals.add(tmp);
		}
		System.out.println("KitchenAgent: Ordinazione pronta per il tavolo " + table);
	}

	@Override
	public boolean done() {
		return false;
	}

}